package com.boot.project.admin.controller.system;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.project.admin.shiro.util.EncryptUtils;
import com.boot.project.core.annotation.AccessLimit;
import com.boot.project.core.annotation.RefuseResponseResult;
import com.boot.project.core.annotation.ResponseResult;
import com.boot.project.core.constant.SystemConstant;
import com.boot.project.core.enums.log.ParamPosition;
import com.boot.project.core.model.system.criteria.UserCriteria;
import com.boot.project.core.model.system.dto.UserAssignDto;
import com.boot.project.core.model.system.dto.UserSaveDto;
import com.boot.project.core.model.system.entity.User;
import com.boot.project.core.model.system.vo.UserVo;
import com.boot.project.module.log.annotation.AccessLog;
import com.boot.project.module.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @description:
 * @author: tsing
 * @create: 2021-01-22 20:35
 */
@RestController
@RequestMapping("/sys/user")
@ResponseResult
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页条件查询
     * @param criteria 查询条件
     * @return
     */
    @GetMapping
    @AccessLog(value = "条件查询用户信息")
    @RefuseResponseResult
    public Page<UserVo> list(UserCriteria criteria) {
        return userService.page(criteria);
    }

    /**
     * 新增用户信息
     * @param userSaveDto 用户信息
     */
    @PostMapping
    @AccessLog(value = "新增用户信息", position = ParamPosition.BODY)
    public void add(@Valid @RequestBody UserSaveDto userSaveDto) {
        String salt = UUID.fastUUID().toString().replaceAll("-", "").toUpperCase();
        String password = EncryptUtils.encrypt(SystemConstant.DEFAULT_PASSWORD, salt);
        userService.add(userSaveDto, salt, password);
    }

    /**
     * 根据id查询用户信息
     * @return
     */
    @GetMapping("/{id}")
    @AccessLog(value = "查询id为[{1}]的用户信息")
    public User findOne(@PathVariable("id") Long id) {
        return userService.findOne(id);
    }

    /**
     * 更新用户信息
     * @param userSaveDto 用户信息
     */
    @PutMapping
    @AccessLog(value = "更新用户信息", position = ParamPosition.BODY)
    public void edit(@Valid @RequestBody UserSaveDto userSaveDto) {
        userService.edit(userSaveDto);
    }

    /**
     * 批量删除用户信息
     * @param ids 用户id的集合
     */
    @DeleteMapping
    @AccessLog(value = "批量删除用户信息")
    public void delete(@RequestParam("ids") List<Long> ids) {
        userService.delete(ids);
    }

    /**
     * 设置用户角色
     * @param dto 设置的信息
     */
    @PutMapping("/assign")
    @AccessLog(value = "设置用户角色", position = ParamPosition.BODY)
    public void assignRole(@Valid @RequestBody UserAssignDto dto) {
        userService.assignRole(dto);
    }
}
